//
//  ViewController.m
//  Test
//
//  Created by sunx on 15/3/17.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "ViewController.h"
#import "NSURLSession+KKAddition.h"
#import "ImageLoard.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *buttom = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    buttom.backgroundColor = [UIColor yellowColor];
    [buttom setTitle:@"new Threed" forState:UIControlStateNormal];
    [buttom addTarget:self action:@selector(downloardImage) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:buttom];
}
- (void)downloardImage {
    [ImageLoard loardImageToDocuments:self.imageView imageURL:@"http://a.hiphotos.baidu.com/image/pic/item/d1a20cf431adcbef56af1b67afaf2edda3cc9f33.jpg"];
}
/*
- (void)downloadImage {
    //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *imageURL = @"http://a.hiphotos.baidu.com/image/pic/item/d1a20cf431adcbef56af1b67afaf2edda3cc9f33.jpg";
        NSString *save = [self applicationDocumentsDirectory];
        NSLog(@"%@",save);
        //NSString *save = @"/Users/sunx/Desktop/";
        [NSURLSession Download:imageURL saveTo:save progress:^(CGFloat progress) {
            
        } completed:^(NSString *image){
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString *path = [[NSString alloc] initWithFormat:@"%@%@",[self applicationDocumentsDirectory],image];
                
                UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];
                
                NSLog(@"conpeleted...image:%@",image);
                
                self.imageView.image = img;
                
            });
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
    //});
}
 */
/*
-(NSString *)applicationDocumentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *result = [[NSString alloc] initWithFormat:@"%@/",docDir];
    return result;
}
 */
    -(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
